package com.cema.manage.project.android.biz;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.digest.MD5;
import com.cema.manage.common.utils.encryption.RSAEncrypt;
import com.cema.manage.project.android.TestBaseContrller;
import com.cema.manage.project.android.model.AdRecordSecondaryVo;
import com.cema.manage.project.android.result.JsonResult;
import com.cema.manage.project.enums.RedisEnum;
import com.cema.manage.project.enums.RiskEnum;
import com.cema.manage.project.manage.adRecordSecondary.domain.AdRecordSecondary;
import com.cema.manage.project.manage.adRecordSecondary.service.IAdRecordSecondaryService;
import com.cema.manage.project.manage.users.domain.Users;
import com.cema.manage.redis.RedisService;
import groovy.transform.Synchronized;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @author mist li
 */
@RequestMapping("secondary")
@RestController
public class BizAdRecordSecondaryController extends TestBaseContrller {
    @Resource
    private IAdRecordSecondaryService adRecordSecondaryService;

    @Resource
    private RedisService redisService;

    private final ReentrantLock lock = new ReentrantLock();

    @PostMapping("add")
    @Synchronized
    public JsonResult adRecordSecondary(HttpServletRequest request, AdRecordSecondaryVo adRecordSecondaryVo) {
        lock.lock();
        try {
            Users users = getUser(request);
            if (ObjectUtil.isNull(users)) {
                return JsonResult.returnResetLogin("登录失效、请重新登录");
            }
//            String bidding = (String) redisService.get(RedisEnum.BIDDING_DEPUTY_GUANG.getKey() + users.getId());
//            if (StrUtil.isNotEmpty(bidding)) {
//                return JsonResult.returnSuccess(RiskEnum.RISK_YES.getValue());
//            }
            MD5 md5 = MD5.create();
            String name = adRecordSecondaryVo.toString() + RSAEncrypt.publicKey;
            String bytes = md5.digestHex(name);
            if (!bytes.equals(adRecordSecondaryVo.getSign())) {
                return JsonResult.returnSuccess(RiskEnum.RISK_YES.getValue());
            }
            //验证配置信息
//            if (StrUtil.isNotEmpty(bidding)) {
//                String[] strs = bidding.split(StrUtil.COMMA);
//                int number = Integer.parseInt(strs[0]);
//                int day = Integer.parseInt(strs[1]);
//                //副广竞价大于等于N金币，间隔N小时
//                if (adRecordSecondaryVo.getArsRealityMoney() >= number) {
//                    redisService.set(RedisEnum.BIDDING_DEPUTY_GUANG.getKey() + users.getId(), day, (long) day, TimeUnit.HOURS);
//                    return JsonResult.returnSuccess(RiskEnum.RISK_YES.getValue());
//                }
//            }
            AdRecordSecondary adRecordSecondary = BeanUtil.toBean(adRecordSecondaryVo, AdRecordSecondary.class);
            adRecordSecondary.setUsId(users.getId());
            adRecordSecondary.setUsNickname(users.getNickname());
            adRecordSecondary.setArsChannel("ad_" + adRecordSecondary.getArsChannel());
            adRecordSecondaryService.insert(adRecordSecondary);
            return JsonResult.returnSuccess(RiskEnum.RISK_NO.getValue());
        } finally {
            lock.unlock();
        }
    }
}
